package com.lu.manage.modular.system.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lu.manage.core.base.controller.BaseController;
import com.lu.manage.core.common.constant.SystemConstant;
import com.lu.manage.core.common.node.ZTreeNode;
import com.lu.manage.core.tips.SuccessTip;
import com.lu.manage.core.utils.ToolUtil;
import com.lu.manage.modular.system.model.Menu;
import com.lu.manage.modular.system.model.dto.MenuDto;
import com.lu.manage.modular.system.service.MenuService;
import com.lu.manage.modular.system.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @program LuBoot
 * @description:
 * @author: zhanglu
 * @create: 2019-10-17 09:11:00
 */
@Controller
@RequestMapping("/system/menu")
public class MenuController extends BaseController {

    private static final String PREFIX = "/modular/system/menu";

    @Autowired
    private MenuService menuService;

    @Autowired
    private UserService userService;

/*******************************跳转页面相关*******************************/

    /**
     * 首页
     * @return
     */
    @GetMapping("")
    public Object index(){
        return PREFIX + "/menu.html";
    }

    /**
     * 新增
     * @return
     */
    @GetMapping("/toAdd")
    public Object toAdd(){
        return PREFIX + "/menu_add.html";
    }

    /**
     * 修改
     * @return
     */
    @GetMapping("/toUpdate")
    public Object toUpdate(String menuId, Model model){

        System.out.println("menuId ---> " + menuId);
        Menu menu = menuService.getById(menuId);

        MenuDto menuDto = new MenuDto();
        BeanUtil.copyProperties(menu, menuDto);

        //设置pid和父级名称
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("code", menu.getPcode());
        Menu pMenu = menuService.getOne(queryWrapper);

        if(ToolUtil.isNotEmpty(pMenu)){
            menuDto.setPid(pMenu.getMenuId());
            menuDto.setPcodeName(pMenu.getName());
        }else {
            menuDto.setPid(SystemConstant.pid);
            menuDto.setPcodeName(SystemConstant.P_NAME);
        }

        model.addAttribute("obj", menuDto);

        return PREFIX + "/menu_update.html";
    }

/*******************************操作按钮相关*******************************/

    /**
     * 新增菜单
     * @param menu
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    @ResponseBody
    @RequiresPermissions(value = "/system/menu/add")
    public Object add(MenuDto menu) throws Exception{

        this.menuService.add(menu);

        return new SuccessTip();
    }

    /**
     * 修改菜单
     * @param menuDto
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    @RequiresPermissions(value = "/system/menu/update")
    public Object update(MenuDto menuDto) throws Exception {

        //如果修改了编号，则该菜单的子菜单也要修改对应的父级(菜单层级修改情况)
        this.menuService.update(menuDto);

        //刷新当前用户菜单
        this.userService.refreshCurrentUser();

        return new SuccessTip();
    }

    /**
     * 修改菜单信息
     * @param data
     * @return
     */
    @PostMapping("/updateInfo")
    @ResponseBody
    @RequiresPermissions(value = "/system/menu/updateInfo")
    public Object updateInfo(String data) throws Exception {
        Menu menu = JSONObject.parseObject(data, Menu.class);
        this.menuService.updateById(menu);
        return new SuccessTip();
    }

    /**
     * 删除菜单
     * @param menuId
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = "/system/menu/delete")
    public Object delete(String menuId) throws Exception{

        this.menuService.deleteMenuSubMenus(menuId);

        return new SuccessTip();
    }

    /**
     * 菜单排序
     * @param nodes
     * @return
     * @throws Exception
     */
    @PostMapping("/sort")
    @ResponseBody
    @RequiresPermissions(value = "/system/menu/sort")
    public Object sort(String nodes) throws Exception{
        this.menuService.sortAllMenus(nodes);
        return new SuccessTip();
    }


/*******************************查询数据相关*******************************/

    /**
     * 菜单树形表格
     * @param searchWords 菜单名称，code
     * @param level
     * @return
     */
    @GetMapping("/listTree")
    @ResponseBody
    public Object listTree(String searchWords, String level){
        //菜单的父子关系
        List<Map<String, Object>> menus = menuService.selectMenuTree(searchWords, level);
        return new SuccessTip(menus);
    }

    /**
     * 菜单树
     * @return
     */
    @GetMapping("/selectMenuTreeList")
    @ResponseBody
    public Object selectMenuTreeList(){
        //菜单的父子关系
        List<ZTreeNode> menus = menuService.menuTreeList();
        menus.add(ZTreeNode.createParent());
        return menus;
    }


    /**
     * 菜单排序树
     * @return
     */
    @GetMapping("/menuSortTreeList")
    @ResponseBody
    public Object menuSortTreeList(){
        //菜单的父子关系
        List<ZTreeNode> menus = menuService.menuTreeList();
        menus.add(ZTreeNode.createParent().setDrag(true).setDropInner(true).setDropRoot(true));
        return menus;
    }

    /**
     * 获取角色的菜单列表
     *
     */
    @GetMapping("/menuTreeListByRoleId/{roleId}")
    @ResponseBody
    public List<ZTreeNode> menuTreeListByRoleId(@PathVariable String roleId) throws Exception {
        List<String> menuIds = this.menuService.getMenuIdsByRoleId(roleId);
        if (ToolUtil.isEmpty(menuIds)) {
            return this.menuService.menuTreeList();
        } else {
            return this.menuService.menuTreeListByMenuIds(menuIds);
        }
    }

}
